<?php
session_start();
require '../config.php';

$nombre= $_REQUEST['nombre'];

$apellido= $_REQUEST['apellido'];
$cedula= $_REQUEST['cedula'];
$filtro= $_REQUEST['filtro'];

if($apellido=='false') $apellido=0;else $apellido=1;
if($cedula=='false') $cedula=0;else $cedula=1;
if($nombre=='false') $nombre=0;else $nombre=1;


if($nombre && $apellido  && $cedula)// 1 2 3
	$query= "select * from clientes where activo = '1' and (nombres LIKE '%".$filtro."%') OR (apellidos LIKE '%".$filtro."%') OR (cedula LIKE '%".$filtro."%')";
else if(!$nombre && $apellido && $cedula)// 2 3
	$query= "select * from clientes where activo = '1' and (apellidos LIKE '%".$filtro."%') OR (cedula LIKE '%".$filtro."%')";
else if(!$nombre && !$apellido && $cedula)// 3
	$query= "select * from clientes where activo = '1' and (cedula LIKE '%".$filtro."%')";
else if($nombre && $apellido && !$cedula)//1 2
	$query= "select * from clientes where activo = '1' and (nombres LIKE '%".$filtro."%') OR (apellidos LIKE '%".$filtro."%')";
else if($nombre && !$apellido  && !$cedula)//1
	$query= "select * from clientes where activo = '1' and nombres LIKE '%".$filtro."%'";
else if(!$nombre && $apellido  && !$cedula)//2
	$query= "select * from clientes where activo = '1' and (apellidos LIKE '%".$filtro."%')";
else if($nombre && !$apellido  && $cedula) //1 3
	$query= "select * from clientes where activo = '1' and (nombres LIKE '%".$filtro."%') OR (cedula LIKE '%".$filtro."%')";
/* else if(!$nombre && $apellido && !$cedula)//2
	$query= "select * from clientes where activo = '1' and (apellidos LIKE '%".$filtro."%')";
else if($nombre && $apellido  && $cedula)//1 2 3
	$query= "select * from clientes where activo = '1' and (nombres LIKE '%".$filtro."%') OR (apellidos LIKE '%".$filtro."%') OR (cedula LIKE '%".$filtro."%')";
 */
$resultset = mysql_query($query,$conexion);
if(!$resultset)
{
    mysql_close();
    echo "<legend>Sin Resultados</legend>";
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
     echo "<legend>Sin Resultados</legend>";
}
else
{
	?>
						
						<button type="button" class="btn btn-default" onclick="reportarpdf();" >Imprimir <span class= 'glyphicon glyphicon-print'></span>  </button>
								<?php
		
			
			echo   "<table class='table table-bordered'>";
			
			echo   "<tr>";
			echo   "<th>Identificador</th><th>Nombre</th><th>Apellido</th><th>C&eacute;dula</th><th>Categor&iacute;a</th><th>Opciones</th>";
			echo   "</tr>";
			echo   "<tbody>";
			$aux=0;
			while($row = mysql_fetch_assoc($resultset))
			{
				if($aux==0){
					$color="#FFFFFF";
					$aux=1;
				}else{
					$color="#F9F9F9";
					$aux=0;
				}
				echo ("<tr bgcolor='".$color."'>");
				echo   "<td>".$row['idclientes']."</td>";
				echo   "<td>".$row['nombres']."</td>";
				echo   "<td>".$row['apellidos']."</td>";
				echo   "<td>".$row['cedula']."</td>";
				echo   "<td>".retornaCategoria($row['categoria'])."</td>";
				if($row['cedula'] != 'anonimo')
				{	
					$result2 = mysql_query("select p.actualizar,p.eliminar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre = 'Clientes'",$conexion);
					if(mysql_num_rows($result2)>0)
					{
						$row2 = mysql_fetch_assoc($result2);
						if($row2['actualizar']== 1 and $row2['eliminar']== 1)
						{
							echo   "<td><input type = button class='btn btn-info btn-xs'  value= Modificar onclick='modificar(". $row['idclientes'] .")'>&nbsp";
							echo   "<input type = button class='btn btn-danger btn-xs' value= Eliminar onclick='eliminar(". $row['idclientes'] .")'></td>";
							
						}
						else if($row2['actualizar']== 1 and $row2['eliminar']== 0)
						{
							echo   "<td><input type = button class='btn btn-info btn-xs'  value= Modificar onclick='modificar(". $row['idclientes'] .")'></td>";
													
						}
						else if($row2['actualizar']== 0 and $row2['eliminar']== 1)
						{
							echo   "<td><input type = button class='btn btn-danger btn-xs' value= Eliminar onclick='eliminar(". $row['idclientes'] .")'></td>";
								
						}
						else echo"<td></td>";
					}														
				}
				echo   "</tr>";
			}
			echo   "</tbody>";
			echo   "</table>";
			mysql_close();
		
}
function retornaProvincia($codigo){ 
	$resultset2 = mysql_query("select PROVINCIA from provincias where idprovincias= ".$codigo);
	$row2 = mysql_fetch_assoc($resultset2);
	return $row2['PROVINCIA'];
} 
function retornaCategoria($codigo){
	$resultset2 = mysql_query("select descripcion from categoriasclientes where idcategoriasclientes= ".$codigo);
	$row2 = mysql_fetch_assoc($resultset2);
	return $row2['descripcion'];
}
function retornaIcono($valor)
{
	if($valor==1) return '<span class= "glyphicon glyphicon-ok-circle"></span>';
	else 
		return '';
}
function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}
?>